package com.bestcem.xm.common.core.config.log;

import ch.qos.logback.core.boolex.EventEvaluatorBase;

/**
 * 1、有错误日志后就发送邮件 2、每次邮件间隔1分钟，连续三次邮件告警后，休息1小时 3、发送邮件告警后清空错误日志队列 4、每封邮件50条
 *
 * @description: 邮件发送频次限制
 * @author: zhenz
 * @date: 2020/3/18 15:29
 */
public class CounterBasedEvaluator extends EventEvaluatorBase {

    /**
     * 发送邮件间隔 1分钟
     */
    private long sendInterval = 60000;

    /**
     * 连续发送次数
     */
    private int continuityCount = 0;

    /**
     * 连续发送次数限制
     */
    private int continuityCountInterval = 3;

    /**
     * 上次发送时间
     */
    private long lastSendTime = 0;

    /**
     * 连续发送三次后休息1小时
     */
    private long waitTime = 3600000;

    /**
     * 下次发送超过1分钟，重置发送次数
     */
    private long threeSendInterval = sendInterval * continuityCountInterval;

    private int counter = 0;
    private String name;
    /**
     * 邮件发送开关
     */
    private boolean emailEnable;

    private void updateLastSendTime() {
        this.lastSendTime = System.currentTimeMillis();
    }

    @Override
    public boolean evaluate(Object event) {
        if (!emailEnable) {
            return false;
        }
        boolean sendFlag = false;

        counter++;

        long current = System.currentTimeMillis();

        // 上次发送的时间差
        long time = current - lastSendTime;

        if (continuityCount > continuityCountInterval) {
            // 连续发送超过三次
            if (time > waitTime) {
                // 离上次发送时间超过一小时
                sendFlag = true;
                // 清空连续发送次数
                continuityCount = 1;
                updateLastSendTime();
            }
        } else {
            // 连续发送未超过三次
            if (time > sendInterval) {
                // 离上次发送时间超过一分钟
                sendFlag = true;
                if (time > threeSendInterval) {
                    // 未超过三次且发送时间间隔超过sendInterval * continuityCount
                    continuityCount = 1;
                } else {
                    continuityCount++;
                }
                updateLastSendTime();
            }
        }

        return sendFlag;
    }

    @Override
    public String getName() {
        return name;
    }

    @Override
    public void setName(String name) {
        this.name = name;
    }

    public long getSendInterval() {
        return sendInterval;
    }

    public void setSendInterval(long sendInterval) {
        this.sendInterval = sendInterval;
    }

    public int getContinuityCount() {
        return continuityCount;
    }

    public void setContinuityCount(int continuityCount) {
        this.continuityCount = continuityCount;
    }

    public int getContinuityCountInterval() {
        return continuityCountInterval;
    }

    public void setContinuityCountInterval(int continuityCountInterval) {
        this.continuityCountInterval = continuityCountInterval;
    }

    public long getLastSendTime() {
        return lastSendTime;
    }

    public void setLastSendTime(long lastSendTime) {
        this.lastSendTime = lastSendTime;
    }

    public long getWaitTime() {
        return waitTime;
    }

    public void setWaitTime(long waitTime) {
        this.waitTime = waitTime;
    }

    public int getCounter() {
        return counter;
    }

    public void setCounter(int counter) {
        this.counter = counter;
    }

    public boolean isEmailEnable() {
        return emailEnable;
    }

    public void setEmailEnable(boolean emailEnable) {
        this.emailEnable = emailEnable;
    }
}
